package com.usian.wemedia.controller.v1;

import com.fasterxml.jackson.databind.ser.std.FileSerializer;
import com.sun.xml.bind.v2.model.core.ID;
import com.usian.api.wemedia.WmNewsControllerApi;
import com.usian.model.admin.dtos.NewsAuthDto;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmNewsDto;
import com.usian.model.media.dtos.WmNewsPageReqDto;
import com.usian.model.media.pojos.WmNews;
import com.usian.model.media.pojos.WmNewsVo;
import com.usian.model.media.pojos.WmUser;
import com.usian.wemedia.mapper.WmNewsMapper;
import com.usian.wemedia.mapper.WmUserMapper;
import com.usian.wemedia.service.WmNewsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController implements WmNewsControllerApi {

    @Autowired
    private WmNewsService wmNewsService;


    @PostMapping("/findList")
    @Override
    public PageResponseResult findList(@RequestBody NewsAuthDto dto){
        return wmNewsService.findListAndPage(dto);
    }

    @GetMapping("/find_news_vo/{id}")
    @Override
    public WmNewsVo findWmNewsVo(@PathVariable("id") Integer id) {
        return wmNewsService.findWmNewsVo(id);
    }

    @GetMapping("/findOne/{id}")
    @Override
    public WmNews findById(@PathVariable("id") Integer id) {
        return wmNewsService.getById(id);
    }

    @PostMapping("/update")
    @Override
    public ResponseResult updateWmNews(@RequestBody WmNews wmNews) {
        wmNewsService.updateById(wmNews);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    @Override
    @GetMapping("/one/{id}")
    public ResponseResult findWmNewsById(@PathVariable("id") Integer id) {
        return wmNewsService.findWmNewsById(id);
    }

    @Override
    @PostMapping("/submit")
    public ResponseResult submitNews(@RequestBody WmNewsDto wmNews) {
        if(wmNews.getStatus()== WmNews.Status.SUBMIT.getCode()){
            //提交文章
            return wmNewsService.saveNews(wmNews, WmNews.Status.SUBMIT.getCode());
        }else{
            //保存草稿
            return wmNewsService.saveNews(wmNews, WmNews.Status.NORMAL.getCode());
        }    }

    @PostMapping("/list")
    @Override
    public ResponseResult findAll(@RequestBody WmNewsPageReqDto wmNewsPageReqDto){
        return wmNewsService.findAll(wmNewsPageReqDto);
    }

    @GetMapping("/del_news/{id}")
    @Override
    public ResponseResult delNews(@PathVariable("id") Integer id) {
        return wmNewsService.delNews(id);
    }

    @PostMapping("/down_or_up")
    @Override
    public ResponseResult downOrUp(@RequestBody WmNewsDto dto) {
        return wmNewsService.downOrUp(dto);
    }

    @Override
    public List<Integer> findRelease() {
        return wmNewsService.findRelease();
    }

//    @Autowired
//    private WmNewsMapper wmNewsMapper;
//    @Override
//    public PageResponseResult findList(NewsAuthDto dto) {
//        dto.checkParam();
//        dto.setPage((dto.getPage()-1)*dto.getSize());
//        dto.setTitle(dto.getTitle());
//
//        List<WmNewsVo> list = wmNewsMapper.findListAndPage(dto);
//        int count = wmNewsMapper.findListCount(dto);
//
//        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), count);
//        responseResult.setData(list);
//        return responseResult;
//    }
//
//    @Autowired
//    private WmUserMapper wmUserMapper;
//
//    @Override
//    public WmNewsVo findWmNewsVo(Integer id) {
//        //1.查询文章信息
//        WmNews wmNews = wmNewsService.getById(id);
//        //2.查询作者
//        WmUser wmUser = null;
//        if(wmNews!=null && wmNews.getUserId() != null){
//            wmUser = wmUserMapper.selectById(wmNews.getUserId());
//        }
//
//        //3.封装vo信息返回
//        WmNewsVo wmNewsVo = new WmNewsVo();
//        BeanUtils.copyProperties(wmNews,wmNewsVo);
//        if(wmUser != null){
//            wmNewsVo.setAuthorName(wmUser.getName());
//        }
//        return wmNewsVo;
//    }

}